package mapEditor.view.assetEdit
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	
	import keyboard.KeyComboCell;
	import keyboard.KeyEvent;
	import keyboard.KeyboardFactory;
	
	import mapEditor.MapEditorManager;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.vo.AssetVO;
	import mapEditor.vo.ToolType;
	
	import utils.DisplayUtil;

	public class AssetAreaEditView extends Sprite
	{
		/**
		 *编辑区大小 
		 */		
		private var _areaRec:Rectangle;
		private var _assetEditItemView:AssetEditItemView;
		private var _moveTimer:Timer=new Timer(80);
		private var _moveSelectType:int;
		
		public function AssetAreaEditView(areaRec:Rectangle)
		{
			_areaRec=areaRec;
			resize();
			
			MapEditorManager.instance.addEventListener(MapEditorEvent.ASSET_ITEM_SELECTED, __onAssetItemSelected);
			KeyboardFactory.Instance.addKeyCombo("assetSelectItemMove");
			KeyboardFactory.Instance.addEventListener(KeyEvent.COMBO_ALL_KEY_DOWN, __onComboAllKeyDown);
			KeyboardFactory.Instance.addEventListener(KeyEvent.KEY_UP, __onKeyUp);
			KeyboardFactory.Instance.createCombosKey("assetSelectItemMove", [Keyboard.UP], Keyboard.UP);
			KeyboardFactory.Instance.createCombosKey("assetSelectItemMove", [Keyboard.DOWN], Keyboard.DOWN);
			KeyboardFactory.Instance.createCombosKey("assetSelectItemMove", [Keyboard.LEFT], Keyboard.LEFT);
			KeyboardFactory.Instance.createCombosKey("assetSelectItemMove", [Keyboard.RIGHT], Keyboard.RIGHT);
		}
		
		private function __onComboAllKeyDown(evt:KeyEvent):void
		{
			var keyComboCell:KeyComboCell=evt.data as KeyComboCell;
			if(keyComboCell.type!="assetSelectItemMove")return;
			
			_moveTimer.addEventListener(TimerEvent.TIMER, moveSelectItem);
			_moveTimer.start();
			
			switch(keyComboCell.data)
			{
				case Keyboard.UP:
					_moveSelectType=1;
					break;
				case Keyboard.DOWN:
					_moveSelectType=2;
					break;
				case Keyboard.LEFT:
					_moveSelectType=3;
					break;
				case Keyboard.RIGHT:
					_moveSelectType=4;
					break;
			}
			moveSelectItem();
		}
		
		private function moveSelectItem(evt:TimerEvent=null):void
		{
			switch(_moveSelectType)
			{
				case 1:
					_assetEditItemView.y-=1;
					break;
				case 2:
					_assetEditItemView.y+=1;
					break;
				case 3:
					_assetEditItemView.x-=1;
					break;
				case 4:
					_assetEditItemView.x+=1;
					break;
			}
			_assetEditItemView.assetVO.registerX=_assetEditItemView.x;
			_assetEditItemView.assetVO.registerY=_assetEditItemView.y;
			_assetEditItemView.assetVO.change();
		}
		
		private function __onKeyUp(evt:KeyEvent):void
		{
			_moveTimer.stop();
			_moveTimer.removeEventListener(TimerEvent.TIMER, moveSelectItem);
		}
		
		private function __onAssetItemSelected(evt:MapEditorEvent):void
		{
			clearAsset();
			_assetEditItemView=new AssetEditItemView(evt.date as AssetVO);
			_assetEditItemView.addEventListener(MouseEvent.MOUSE_DOWN, __onEditItemMouseEvent);
			_assetEditItemView.addEventListener(MouseEvent.MOUSE_UP, __onEditItemMouseEvent);
			_assetEditItemView.assetVO.addEventListener(MapEditorEvent.ASSET_ITEM_PROPERTY_CHANGE, __onEditItemPropertyChange);
			addChild(_assetEditItemView);
			setEditItem();
		}
		
		private function setEditItem():void
		{
			_assetEditItemView.x=_assetEditItemView.assetVO.registerX;
			_assetEditItemView.y=_assetEditItemView.assetVO.registerY;
			_assetEditItemView.z=_assetEditItemView.assetVO.registerZ;
		}
		
		private function __onEditItemPropertyChange(evt:MapEditorEvent):void
		{
			setEditItem();
		}
		
		private function __onEditItemMouseEvent(evt:MouseEvent):void
		{
			switch(evt.type)
			{
				case MouseEvent.MOUSE_DOWN:
					switch(MapEditorManager.instance.mapEditorModel.toolType)
					{
						case ToolType.TOOL_SELECT:
							_assetEditItemView.startDrag();
							break;
					}
					break;
				case MouseEvent.MOUSE_UP:
					_assetEditItemView.stopDrag();
					_assetEditItemView.assetVO.registerX=_assetEditItemView.x;
					_assetEditItemView.assetVO.registerY=_assetEditItemView.y;
					_assetEditItemView.assetVO.change();
					break;				
			}
		}
		
		private function clearAsset():void
		{
			if(_assetEditItemView)
			{
				_assetEditItemView.removeEventListener(MouseEvent.MOUSE_DOWN, __onEditItemMouseEvent);
				_assetEditItemView.removeEventListener(MouseEvent.MOUSE_UP, __onEditItemMouseEvent);
				_assetEditItemView.assetVO.removeEventListener(MapEditorEvent.ASSET_ITEM_PROPERTY_CHANGE, __onEditItemPropertyChange);
				DisplayUtil.dispose(_assetEditItemView);
				_assetEditItemView=null;
			}
		}
		
		private function drawEditArea():void
		{
			graphics.clear();
			graphics.lineStyle(1, 0x666666, 0.5);
			graphics.moveTo(-_areaRec.width/2, 0);
			graphics.lineTo(_areaRec.width/2, 0);
			graphics.moveTo(0, -_areaRec.height/2);
			graphics.lineTo(0, _areaRec.height/2);
			graphics.endFill();
		}
		
		public function resize():void
		{
			drawEditArea();
		}
		
		public function dispose():void
		{
			MapEditorManager.instance.removeEventListener(MapEditorEvent.ASSET_ITEM_SELECTED, __onAssetItemSelected);
			KeyboardFactory.Instance.removeEventListener(KeyEvent.COMBO_ALL_KEY_DOWN, __onComboAllKeyDown);
			KeyboardFactory.Instance.removeEventListener(KeyEvent.KEY_UP, __onKeyUp);
			KeyboardFactory.Instance.removeKeyCombo("assetSelectItemMove");
			if(_moveTimer)
			{
				_moveTimer.stop();
				_moveTimer.removeEventListener(TimerEvent.TIMER, moveSelectItem);
				_moveTimer=null;
			}
			_areaRec=null;
			clearAsset();
			if(parent)parent.removeChild(this);
		}
	}
}